package net.zjitc.servlet.product;

import net.zjitc.domain.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hasee on 2017/10/19.
 */
@WebServlet(name = "RemoveCartServlet",urlPatterns = "/removeCartServlet")
public class RemoveCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*int index = Integer.valueOf(request.getParameter("index"));
        List cart = (List) request.getSession().getAttribute("cart");
        cart.remove(index-1);
        request.getSession().setAttribute("cart",cart);*/

        String pid = request.getParameter("pid");
        List cart = (List) request.getSession().getAttribute("cart");
        Iterator iterator = cart.iterator();
        while (iterator.hasNext()){
            Product product = (Product) iterator.next();
            if(pid.equals(product.getPid())){
                iterator.remove();
                break;
            }
        }

        response.sendRedirect("/newweb/cart/cartlist.jsp");
    }
}
